package com.financing.platform.web.api;

import java.io.File;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.FpFaOfflineUserDTO;
import com.financing.platform.service.IOfflineUserService;
import com.financing.platform.util.CsvExportUtil;
import com.financing.platform.util.DTOdateSet;

@Controller("offlineUserActionApi")
@RequestMapping("/api/offlineUser")
@Scope("prototype")
public class OfflineUserAction  extends BaseAction{
	
	@Autowired
	private IOfflineUserService offlineUserSericeImpl;
	
	@Value("#{settings['fileBaseDir']}")
	private String fileBaseDir;

	@Value("#{settings['AliyunOss']}")
	private Boolean aliyunOss;
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/queryOfflineUserList.shtml")
	@ResponseBody
	public ResultMap queryOfflineUserList(HttpServletRequest request,
			HttpServletResponse response,FpFaOfflineUserDTO bean,Pager pager) {
		/**
		 * 返回结果
		 */
		ResultMap resultMap = new ResultMap();
		
		try{
			
			bean.setBegin(pager.getStartIndex());
			bean.setPageSize(pager.getPageSize());
			
			int count = offlineUserSericeImpl.queryFpFaOfflineUserCount(bean);
			pager.setTotalCount(count);
			
			List<FpFaOfflineUserDTO> list = null;
			if(count>0){
				
				list = 	offlineUserSericeImpl.queryFpFaOfflineUserList(bean);
				
				
				for (FpFaOfflineUserDTO fpFaOfflineUserDTO : list) {
					if("1".equals(bean.getRegistrationStatus())){
						fpFaOfflineUserDTO.setRegistrationStatus("已注册");
					}else if("0".equals(bean.getRegistrationStatus())){
						fpFaOfflineUserDTO.setRegistrationStatus("未注册");
					}else {

						String userId = offlineUserSericeImpl.queryFpFaUser(fpFaOfflineUserDTO.getMobile());

						if (StringUtils.isBlank(userId)) {
							fpFaOfflineUserDTO.setRegistrationStatus("未注册");
						} else {
							fpFaOfflineUserDTO.setRegistrationStatus("已注册");
						}
					}
				}
				
			}else{
				list = Collections.EMPTY_LIST;
			}
			
			resultMap.setAttribute("list",list);
			resultMap.setAttribute("pager",pager);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("queryOfflineUserList error",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;
	}
	@RequestMapping("/importOfflineUser.shtml")
	@ResponseBody
	public ResultMap importOfflineUser(HttpServletRequest request,
			HttpServletResponse response,String path) {
		/**
		 * 返回结果
		 */
		ResultMap resultMap = new ResultMap();
		
		try{
			
			//判断 文件类型
			if(!(path.endsWith(".xls"))){
				resultMap.setStatus(STATUS.ERROR);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_FILE_TYPE_ERROR);
    			return  resultMap;
			}
			//判断 是否-oss
			log.info("fileUpload.shtml  AliyunOss:"+aliyunOss);
			List<FpFaOfflineUserDTO> list = null;
			if(!aliyunOss) {
				list = CsvExportUtil.doXlsImportOfflineUser(fileBaseDir+path);
			}else{
				list = CsvExportUtil.doXlsImportOfflineUser(path,true);
			}

			resultMap.setAttribute("list",list);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("importOfflineUser error",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;
	}
	
	@RequestMapping("/importOfflineUserConfirm.shtml")
	@ResponseBody
	public ResultMap importOfflineUserConfirm(HttpServletRequest request,
			HttpServletResponse response,String path) {
		/**
		 * 返回结果
		 */
		ResultMap resultMap = new ResultMap();
		
		try{
			
			//判断 文件类型
			if(!path.endsWith(".xls")){
				resultMap.setStatus(STATUS.ERROR);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_FILE_TYPE_ERROR);
    			return  resultMap;
			}


			//判断 是否-oss
			log.info("fileUpload.shtml  AliyunOss:"+aliyunOss);
			List<FpFaOfflineUserDTO> list = null;
			if(!aliyunOss) {
				list = CsvExportUtil.doXlsImportOfflineUser(fileBaseDir+path);
			}else{
				list = CsvExportUtil.doXlsImportOfflineUser(path,true);
			}

			for (int i = 0; i < list.size(); i++) {
				FpFaOfflineUserDTO  bean = list.get(i);
				DTOdateSet.setCreateInfo(bean, request.getSession());
				DTOdateSet.setUpdateInfo(bean, request.getSession());
				
				try{
					offlineUserSericeImpl.insertOfflineUser(bean);
				}catch(Exception e){
					log.error("insertOfflineUser serice error",e);
				}
			}
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("importOfflineUser error",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;
	}
	@RequestMapping("/queryOfflineUserInfo.shtml")
	@ResponseBody
	public ResultMap queryOfflineUserInfo(HttpServletRequest request,
			HttpServletResponse response,Integer id) {
		/**
		 * 返回结果
		 */
		ResultMap resultMap = new ResultMap();
		
		try{
			FpFaOfflineUserDTO bean = new FpFaOfflineUserDTO();
			bean.setId(id);
			bean =  offlineUserSericeImpl.loadById(bean);
			resultMap.setAttribute("bean",bean);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("queryOfflineUserList error",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;
	}
	@RequestMapping("/deleteOfflineUser.shtml")
	@ResponseBody
	public ResultMap deleteOfflineUser(HttpServletRequest request,
			HttpServletResponse response,Integer id) {
		/**
		 * 返回结果
		 */
		ResultMap resultMap = new ResultMap();
		
		try{
			FpFaOfflineUserDTO bean = new FpFaOfflineUserDTO();
			bean.setId(id);
			bean.setDeleteTag("2");
			DTOdateSet.setUpdateInfo(bean, request.getSession());
			offlineUserSericeImpl.updateFpFaOfflineUser(bean);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("queryOfflineUserList error",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;
	}
	@RequestMapping("/updateOfflineUser.shtml")
	@ResponseBody
	public ResultMap updateOfflineUser(HttpServletRequest request,
			HttpServletResponse response,FpFaOfflineUserDTO bean) {
		/**
		 * 返回结果
		 */
		ResultMap resultMap = new ResultMap();
		
		try{
			offlineUserSericeImpl.updateFpFaOfflineUser(bean);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("updateOfflineUser error",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;
	}
	@RequestMapping("/addOfflineUser.shtml")
	@ResponseBody
	public ResultMap addOfflineUser(HttpServletRequest request,
			HttpServletResponse response,FpFaOfflineUserDTO  bean) {
		/**
		 * 返回结果
		 */
		ResultMap resultMap = new ResultMap();
		
		try{
			if(StringUtils.isBlank(bean.getMobile())){
				
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			DTOdateSet.setCreateInfo(bean, request.getSession());
			DTOdateSet.setUpdateInfo(bean, request.getSession());
			offlineUserSericeImpl.insertOfflineUser(bean);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("addOfflineUser error",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;
	}
}
